package com.huangsm.book.designmodel.kilim;

import kilim.ExitMsg;
import kilim.Mailbox;
import kilim.Pausable;
import kilim.Task;

/**
 * @author huang
 * @PACKAGE_NAME com.huangsm.book.designmodel.kilim
 * @PROJECT_NAME concurrence
 * @date 2019/2/21
 */
public class KilimTest extends Task {
    static Mailbox<String> mb = new Mailbox<>();
    static Mailbox<ExitMsg> exitmb = new Mailbox<>();
    int type = 0;

    public KilimTest(int type) {
        this.type = type;
    }

    public static void main(String[] args) {
        Task sender = new KilimTest(1).start();
        Task reciever = new KilimTest(0).start();
        //要求接受者在结束后，填写退出邮箱
        reciever.informOnExit(exitmb);
        //退出邮箱中的内容，表示接受者已经结束
        exitmb.getb();
        System.exit(0);



    }

    @Override
    public void execute() throws Pausable {
        //接受者
        if (type==0)
        {
            while (true){
                //取得邮箱中的信息
                String s = mb.get();
                //如果是结束标识
                if (s.equals("over"))
                {
                    break;
                }
                System.out.println(s);
            }
            //发送者
        }else if (type==1){
            //向邮箱中传递信息
            mb.putnb("hello");
            mb.putnb("world\n");
            mb.putnb("over");
        }
    }
}
